package com.by.sql;

/**
 * @version 1.0
 * @Author 彭彦彬
 * @Date 2025/4/9 11:00
 * @注释
 */


import com.by.utils.StringUtils;

import java.lang.reflect.Field;

public class SqlBuilder {
    public static String buildInsertSql(Class<?> clazz) {
        StringBuilder sql = new StringBuilder("INSERT INTO ");
        String tableName = getTableName(clazz);
        sql.append(tableName).append(" (");
        Field[] fields = clazz.getDeclaredFields();
        for (int i = 0; i < fields.length; i++) {
            sql.append(fields[i].getName());
            if (i != fields.length - 1) sql.append(", ");
        }
        sql.append(") VALUES (");
        for (int i = 0; i < fields.length; i++) {
            sql.append("?");
            if (i != fields.length - 1) sql.append(", ");
        }
        sql.append(")");
        return sql.toString();
    }

    public static String buildSelectByIdSql(Class<?> clazz) {
        String tableName = getTableName(clazz);
        return "SELECT * FROM " + tableName + " WHERE id = ?";
    }

    public static String buildSelectAllSql(Class<?> clazz) {
        String tableName = getTableName(clazz);
        return "SELECT * FROM " + tableName;
    }

    private static String getTableName(Class<?> clazz) {
        // 简单实现：将类名转换为下划线命名法

        return StringUtils.camelToUnderline(clazz.getSimpleName());
    }
}
